package cn.dansj.handler.interfaces;

import cn.dansj.annotation.MarkAsApi;
import cn.dansj.common.utils.bean.CacheToken;
import cn.dansj.entity.User;
import cn.dansj.common.utils.enums.ResponseCode;
import cn.dansj.exception.InterfaceResponseException;
import cn.dansj.service.UserService;
import cn.dansj.common.context.ApplicationContextProvider;
import cn.dansj.common.request.RequestUtils;
import cn.dansj.common.utils.transfer.Transformation;
import cn.dansj.handler.InterfaceHandler;
import cn.dansj.utils.project.ProjectUtils;

import java.util.Map;

@SuppressWarnings("all")
@MarkAsApi
public class ExchangeToken extends InterfaceHandler {
    private static final UserService userService = ApplicationContextProvider.getBean(UserService.class);

    @Override
    public Object execute(Map<String, Object> paramsMap) {
        try {
            final CacheToken eToken = ProjectUtils.getCacheToken(ProjectUtils.decryptByPrivateKey(Transformation.toString(paramsMap.get("eToken"))));
            final User user = userService.getUserBySequence(eToken.getSequence());
            RequestUtils.getRequest().setAttribute("currentUsername", user.getUsername());
            RequestUtils.getRequest().setAttribute("platform","fromConsole");
            return ProjectUtils.initUserLoginData(user).getData();
        } catch (Exception exception) {
            throw new InterfaceResponseException(ResponseCode.NotExist);
        }
    }
}
